/** 
 * User: seeker910
 * Date: 13-10-24
 * Time: 下午10:23 
 * @description label 是一个具有编辑功能的控件，通过 readonly=true ,和 editor属性控制 编辑状态
 * */
Rsd.define('Rsd.form.Label', {
    extend: 'Rsd.control.Component',
    xtype: 'label',
    /*
    * 文本信息
    * */
    //text:'',
    ctrlTagName: 'nobr',
    cls:'x-control-label',
    ctrlCls:'x-ctrl',
    dataIndex:'',
    /*
    * text 文本颜色
    * */
    color:null,
    height:30,
    mulitiLine:false,
    /**
     * 可编辑
     */
    editable:false,
    /**
     * {
     *     title:'',//修改提示
     *     tip:'',//同 title
     *     text:''.//修改按钮文字
     *     handler:''//点击修改按钮响应事件
     *     saveHandler:'',//保存按钮响应事件
     *     saveText:'',//保存按钮文字
     *     saveTitle:'',//保存按钮提示
     *     cancelHandler:'',//取消按钮响应事件
     *     cancelText:'',//取消按钮文字
     *     cancelTitle:''//取消按钮提示
     *     
     * }
     */
    //editor:{},
    /*
    * */
    constructor: function Label(config) {
        config = config || {};
        this.apply(config);
    },
    /**
     * 
     */
    initComponentEx: function initComponentEx() 
    {
        this.callParent();
        var me = this;
         
        if (this.editable === true) {
            var me = this;
            var _editor = me.editor;
            if(!_editor.viewerType)
            {
                //默认编辑控件 
                var element = document.createElement(me.editor.elementType||'input');
                _editor.element = element; 
                element.id = this.id + '__editor'; 
                element.title = _editor.tip || _editor.title || '';
                me.setElStyle(element, _editor.style || {});
                
                element.style.flex = 1;
                element.style.marginRight = "8px";
                element.style.marginLeft = "10px";
                element.style.float = "right";
                element.style.position = "relative";
                element.style.display = "none";
                element.style.zIndex = 9999;
                element.style.height = me.height + 'px';
                element.style.resize = 'none';

                //保存按钮
                var btnSave = document.createElement('a');
                _editor.saveElement = btnSave;

                btnSave.id = this.id + '__save';
                btnSave.innerHTML = _editor.saveText || Rsd.lang('form.label.editor.saveButtonText');
                btnSave.title = _editor.cancelTip || _editor.cancleTitle || '';
                btnSave.style.marginRight = "8px";
                btnSave.style.marginLeft = "0px";
                btnSave.style.float = "right";
                btnSave.style.position = "relative";
                btnSave.style.display = "none";
                btnSave.style.zIndex = 9999;
                btnSave.style.lineHeight = me.height + 'px';
                btnSave.href = "javascript:void(0);"
                btnSave.onclick = function (event) { 
                     
                    if (_editor.saveHandler || _editor.saveFn) {
                        
                        me.funApplyByIOC(_editor.saveHandler || _editor.saveFn, [me, event]);
                    }
                    else
                    {
                        console.log('property saveHandler is null in ' + me.dataIndex);
                    }
                    //显示ctrl
                    me.ctrl.element.style.display = "";
                    //隐藏编辑器
                    _editor.element.style.display="none";

                    //显示 修改
                    _editor.updateElement.style.display=""; 
                    //隐藏 保存
                    _editor.saveElement.style.display="none"; 
                    //隐藏 取消
                    _editor.cancelElement.style.display="none";  
                }
            
                //取消按钮
                var btnCancel = document.createElement('a');
                _editor.cancelElement = btnCancel;

                btnCancel.id = this.id + '__cancel';
                btnCancel.innerHTML = _editor.cancelText || Rsd.lang('form.label.editor.cancelButtonText');
                btnCancel.title = _editor.cancelTip || _editor.cancleTitle || '';
                btnCancel.style.marginRight = "8px";
                btnCancel.style.marginLeft = "0px";
                btnCancel.style.float = "right";
                btnCancel.style.position = "relative";
                btnCancel.style.display = "none";
                btnCancel.style.zIndex = 9999;
                btnCancel.style.lineHeight = me.height + 'px';
                btnCancel.href = "javascript:void(0);"
                btnCancel.onclick = function (event) {
                    if (_editor.cancelHandler || _editor.cancelFn) {
                        me.funApplyByIOC(_editor.cancelHandler || _editor.cancelFn, [me, event]); 
                    }
                    
                    //显示ctrl
                    me.ctrl.element.style.display = "";
                    //隐藏编辑器
                    _editor.element.style.display="none";

                    //显示 修改
                    _editor.updateElement.style.display=""; 
                    //隐藏 保存
                    _editor.saveElement.style.display="none"; 
                    //隐藏 取消
                    _editor.cancelElement.style.display="none";
                }
            }
             
            //修改按钮
            var btnUpdate = document.createElement('a');
            _editor.updateElement = btnUpdate; 
            btnUpdate.id = this.id + '__update';
            btnUpdate.innerHTML = _editor.text || Rsd.lang('form.label.editor.editorButtonText');
            btnUpdate.title = _editor.tip || _editor.title || '';
            btnUpdate.style.marginRight = "8px";
            btnUpdate.style.marginLeft = "10px";
            btnUpdate.style.float = "right";
            btnUpdate.style.position = "relative";
            btnUpdate.style.zIndex = 9999;
            btnUpdate.style.lineHeight = me.height + 'px';
            btnUpdate.href = "javascript:void(0);";
            btnUpdate.onclick = function (event) {
               
                if (_editor.handler || _editor.fn) {
                    me.funApplyByIOC(_editor.handler || _editor.fn, [me, event]);
                    return;
                } 
                if (_editor.viewerType) {
                     
                    Rsd.create(_editor.viewerType, {
                        readonly: true,
                        autoLoad: true,
                        title: _editor.title || ('编辑-' + me.label.content),
                        modelType: _editor.modelType, 
                        width: _editor.width || 800,
                        height: _editor.height || 600,  
                        style:_editor.style || {},
                        parent: me,  

                    }).showDialog();
                    return;
                }
                //隐藏ctrl
                 me.ctrl.element.style.display = "none";
                //显示编辑器
                 _editor.element.style.display="";
                _editor.element.value = me.getValue();
                //隐藏 修改
                _editor.updateElement.style.display="none";
                //显示 保存
                _editor.saveElement.style.display="";  
                //显示 取消
                _editor.cancelElement.style.display=""; 

                return;
            } 
             
        } 
    },
    /*
    * */
    onBeforeInit:function onBeforeInit() {

        if(this.mulitiLine && this.ctrlTagName=='nobr')
        {   
            this.ctrlTagName = 'p';
            this.ctrl.tagName = 'p';
            this.height=null;
        }
        
        this.callParent();
    },
    /*
     * */
    onAfterInit: function onAfterInit() {
        var me = this;

        this.setText (me.text||me.value); 
        this.callParent(); 
    },
    /**
      * 
      */
     onAfterRender:function onAfterRender()
     {
        this.callParent();

        if(this.editor)
        { 
            if(this.editor.element)
            {
                this.dom.appendChild(this.editor.element);//默认编辑控件
            }
            if(this.editor.updateElement)
            {
                this.dom.appendChild(this.editor.updateElement);//修改按钮
            }
            if(this.editor.saveElement)
            { 
                this.dom.appendChild(this.editor.saveElement);//保存按钮
            }
            if(this.editor.cancelElement)
            { 
                this.dom.appendChild(this.editor.cancelElement);//取消按钮
            }
        }
 
     },
    /**
     * 
     */
    onAfterLayout:function onAfterLayout()
    {
        if(this.mulitiLine==false && (this.label.position == 'left' || this.label.position =='right') && Rsd.isNumber(this.height))
        {
            this.ctrl.element.style.lineHeight = this.height+ 'px';
            if(this.label.element)
            {
                this.label.element.style.lineHeight = this.height+ 'px';
            }
        }
        this.callParent();
    },
    /*
     * */
    getText: function () {
        return  Rsd.isNullOrUndefined(this.__text)?this.value:this.__text;
    },

    /*
     * */
    setText: function (text) {
        var me = this;
        me.__text = Rsd.isNullOrUndefined(text)? '':text;
        if (me.ctrl.element) {
            if( me.ctrl.element.removeChild)
            {
               while(me.ctrl.element.childNodes.length > 0)
               {
                  me.ctrl.element.removeChild(me.ctrl.element.childNodes[0]);
               }
            }

           me.ctrl.element.appendChild(document.createTextNode(me.text));
            if(me.color)
            {
                me.ctrl.element.style.color = me.color;
            }
        }
    },
    /**
     * 
     * @param {*} value 
     */
    setValue:function setValue(value) {
        this.setText(value);
    },
    /***
     * 
     */
    getValue:function getValue() {
        return this.getText();
    }


},function (type) {

    var _textGetter = function () {
        return this.getText();
    };

    var _textSetter = function (value) {
        this.setText(value);
    };
    this.defineProperty(type,"text", _textGetter, _textSetter,true);

    var _editorGetter = function () {

        if(!this.___editor)
        {
            this.___editor = {style:null, text:Rsd.lang('form.label.editor.editButtonText'), handler:''}; 
        }
        return this.___editor;
    };

    var _editorSetter = function (value) {
         
        this.___editor = Rsd.apply({style:null, text:Rsd.lang('form.label.editor.editButtonText'), handler:''},value||{}); 
    }

    this.defineProperty(type,"editor", _editorGetter, _editorSetter,false);
});
